using AiQiuQuan.Sport.Core;
using AiQiuQuan.Sport.WebApi.Core;
using AiQiuQuan.Sport.WebApi.DataDbContext;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

//host config
builder.WebHost.UseContentRoot(Directory.GetCurrentDirectory());
builder.Host.UseAspect();
builder.Host.UseLog<Program>(builder.Logging);

//inject servie
builder.Services.AddServiceCore<AiQiuQuanDbContext>(builder.Configuration);
builder.Services.AddSingleton<ICurrentInfo, CurrentInfo>();
builder.Services.AddCorpAuthentication(builder.Configuration);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(genOption =>
{
    genOption.SchemaFilter<EnumDocumentSchemaFilter>();
    genOption.DocumentFilter<EnumDocumentFilter>();
    genOption.UseInlineDefinitionsForEnums();
    genOption.AddSecurityDefinition("token", new OpenApiSecurityScheme { Name = "Authorization", Type = SecuritySchemeType.ApiKey, In = ParameterLocation.Header });
    foreach (var item in Directory.GetFiles(AppContext.BaseDirectory, "AiQiuQuan.Sport.*.xml"))
    {
        genOption.IncludeXmlComments(item, true);
    }
});

var app = builder.Build();

// pipeline middleware
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(uiOption =>
    {
        uiOption.SwaggerEndpoint("v1/swagger.json", "v1");
        uiOption.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
        uiOption.EnableFilter();
    });
}

app.UseRedLock(app.Lifetime);
app.UseCors("aiqiuquan");
app.MapGet("/v2/health", async (context) =>
{
    await context.Response.WriteAsync($"{context.Request.Host}: {DateTimeOffset.Now:yyyy-MM-dd HH:MM:ss zzz}");
});

app.UseLoggerMiddleware();
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseRedisHangfire("/v2/job");
app.MapControllers();

app.Run();
